import { mainStore } from '@/store/mainStore.js'
import { Float32BufferAttribute } from 'three'
const store = mainStore()
export const exportPoints = () => {
  // const pointCloudData = new Float32BufferAttribute(store.pcdData, 3)
  // console.log(pointCloudData)
  let positions = window.ThreePerceivedEffect.points.geometry.attributes.position.array;
  const pointCloudData = [];
  for (let i = 0; i < positions.length; i += 3) {
      const point = {
        x: positions[i]*10,
        y: positions[i + 1]*10,
        z: positions[i + 2]
      }
      // new THREE.Vector3(, positions[i + 1], positions[i + 2]);
      pointCloudData.push(point);
  }
  // 创建一个2000x2000的Canvas元素
  const canvas = document.createElement('canvas');
  canvas.width = 2000;
  canvas.height = 2000;
  // 获取2D绘图上下文
  const ctx = canvas.getContext('2d');
  // 将坐标原点移动到Canvas的中心
  ctx.translate(canvas.width / 2, canvas.height / 2);
  // 设置点的样式
  ctx.fillStyle = 'black';
  ctx.lineWidth = 1;
  // 绘制点云数据
  pointCloudData.forEach(point => {
    ctx.beginPath();
    ctx.fillStyle = calculateColor(point.z); // 设置点的颜色
    ctx.arc(point.x, -point.y, 1, 0, Math.PI * 2); // 绘制圆形点，注意y坐标取反
    ctx.fill();
  });
  
  // 导出图片
  const dataURL = canvas.toDataURL('image/png');
  const link = document.createElement('a');
  link.href = dataURL;
  link.download = 'pointCloud.bmp';
  document.body.appendChild(link);
  link.click();
  document.body.removeChild(link);
}
const calculateColor = (zValue) => {
  let color;
  if (zValue < 1.0 && zValue > -5.25) {
    const t = (zValue + 5.25) / (1.0 + 5.25); // 映射到 0-1
    color = `rgb(${Math.round((1.0 - t) * 255)}, ${Math.round(t * 255)}, 0)`; // 渐变色
  } else {
    color = 'rgb(128, 0, 128)'; // 默认紫色
  }
  return color;
};
